#include "ScoreCounter.h"
#include <sstream>

ScoreCounter::ScoreCounter(Playfield &pl):
	playfield(pl)
{
	// reset points to zero
	reset();

	x = playfield.getWidth()/2;
	y = 30;
}


ScoreCounter::~ScoreCounter(void)
{
}

void ScoreCounter::reset()
{
	leftPlayersPoints = 0;
	rightPlayersPoints = 0;
}
void ScoreCounter::update(const double time)
{

}

void ScoreCounter::getReadyForDraw()
{

}

void ScoreCounter::scoredPoint(PlayerPosition pos)
{
	if(pos == PP_LEFT)
		leftPlayersPoints++;
	else if (pos == PP_RIGHT)
		rightPlayersPoints++;

	buildString();
	
}

void ScoreCounter::buildString()
{
	std::string leftPlayersString;
	std::string rightPlayersString;

	// We need to convert our player's points to a string, the best (ISO!) way I've found to do this is 
	// by using stringstreams. This is because itoa is NOT a standard C++ function!
	std::stringstream ss;
	ss << leftPlayersPoints;
	ss >> leftPlayersString;

	ss.clear();
	ss << rightPlayersPoints;
	ss >> rightPlayersString;

	// Now, if the string is only one digit, we'll need to add a space before the left string and after
	// the right one, or it'll look funny (because the score 0 - 0 wouldn't be symmetrical. )
	if(leftPlayersPoints < 10)
		leftPlayersString = std::string(" ") + leftPlayersString;

	scoreString = leftPlayersString + " - " + rightPlayersString;
}